Ext.onReady(function(){
	
	Ext.QuickTips.init();
	new Ext.Viewport({
		id:'mainid',
		layout: 'border',
	    defaults: {
	        activeItem: 0
	    },
	    items: [
		/**
		{
	        region: 'north',
			height: 26
   		},
		**/
		{
			region: 'west',
			id:'mytree',
			collapsible: false,
			title: '事件递交系统',
			iconCls:'abc',
       		animCollapse:false,
       		animate: false,
			singleClickExpand:true,
			xtype: 'treepanel',
			width: 250,
			autoScroll: true,
			split: true,
			collapseMode:'mini',

			tbar:['->', { 
					iconCls      : 'icon-expand-all',
					text     : '全部展开',
					listeners   : {'click'  : function(){Ext.getCmp('mytree').root.expand(true); } }
				}, { 
					iconCls      : 'icon-collapse-all',
					text     : '合拢',
					listeners   : {'click'  : function(){Ext.getCmp('mytree').root.collapse(true); } }
			},  { 
					text: "退出",
					iconCls: "exit-button",
					handler: function() {
						window.location.href='/welcome/logout';
					}
			}],

			loader: new Ext.tree.TreeLoader({
				dataUrl: base_url + '/welcome/menu'
			}),
			rootVisible: true,
        
			root: new Ext.tree.AsyncTreeNode({
				text:'统一事件服务平台',
				id: "root",
				singleClickExpand: true,
				expanded:true,
				expanded: true
			}),
            
			listeners: {
				click: function(item) {
					if(item.leaf){
						if( 'exit.php' == item.attributes.dataurl ){
							window.location.href='../exit.php';
						}
						else{
							leaf_dataurl= item.attributes.dataurl;
							if(leaf_dataurl.indexOf('CICICI')==0 ){
								leaf_dataurl= leaf_dataurl.substring(6,leaf_dataurl.length-4);
							}
							/**记录叶子节点cookie**/
							var cp = new Ext.state.CookieProvider();
							var cookieTreeData= item.getPath()+ '..--.-..-...-'+ leaf_dataurl + '..--.-..-...-' + item.attributes.text ;
							var cookieTreeName= 'treeLeaf_'+ cp.get('nowMenuId');
							cp.set( cookieTreeName, cookieTreeData );
							
							var treedata=item.getPath()+'S..AI...S'+cp.get('nowMenuId');

							createNewWin( leaf_dataurl , item.attributes.text , treedata );
						}
	                }
	            }
	        }
	    }, 
		{
			region: 'center',
			id:'mytabs',
			xtype: 'tabpanel',
			plugins: new Ext.ux.TabCloseMenu(),
			enableTabScroll:true,
			items:[{
				title: '主页',
				id:'hometab',
				html: '<iframe width="100%" id="iframe_home" name="iframe_home" height="100%" src="/admin" scrolling="yes" marginwidth="0" frameborder="0" style="z-index:-10;""></iframe>',
				closable:false,
				listeners   : {'activate'  : function(){ setTimeout("setIframeEvent('iframe_home')" , 500) } }
			}]
	    	}]
    });
    
});

/**
 * 设置iframe事件，针对快捷键
 */
function setIframeEvent( id ){
	if(window[id]){
		window[id].document.onkeydown = function(event){
			var evt = event || window[id].event;
			dockeydown(evt);
		}
		window[id].document.onkeyup = function(event){
			var evt = event || window[id].event;
			dockeyup(evt);
		}
	}
}


/**
 * 打开窗口
 */
function createNewWin( item_dataurl , win_text , treedata ){

//	alert(treedata);
	if(item_dataurl.indexOf('http:') >=0 ){
		dataurl= item_dataurl;
	}
	else{
		dataurl= '../' + item_dataurl;
	}
	
	keyid = Ext.id();
	Ext.getCmp('mytabs').activate('hometab');
	Ext.getCmp('mytabs').remove( item_dataurl );
	Ext.getCmp('mytabs').add({
        id:item_dataurl,
        title: win_text,
	treedata:treedata,
        html: '<iframe width="100%" id="'+keyid+'" name="'+keyid+'" height="100%" src="' + dataurl + '" scrolling="yes" marginwidth="0" frameborder="0" style="z-index:-11;"></iframe>',
        closable:true,
		listeners   : {
			'activate'  : 
				function(item){ 
					var treedata= item.treedata.split('S..AI...S');
					var cp = new Ext.state.CookieProvider();
					if( treedata[1] == cp.get('nowMenuId') ){
						Ext.getCmp('mytree').selectPath( treedata[0] );
					}
					else{
						
						cp.set('nowMenuId',treedata[1]);
					}

					setTimeout("setIframeEvent('"+keyid+"')" , 100) 
				} 
		}
    }).show();
}
function dockeydown(event){
	switch(event.keyCode){

	case 17:	//ctrl
		keyctrldown=true;
		break;
	case 77:	//M
		keyctrldown && win && divMenuShow();
		break;
	}
}
function dockeyup(event){
	switch(event.keyCode){
	case 17:	//ctrl
		keyctrldown=false;
		break;
	}
}









